package com.qisi.pojo;

import com.qisi.pojo.alertManager.AlertManagerData;
import com.qisi.pojo.alertManager.Route;
import com.qisi.pojo.alertManager.SentryAlertData;
import com.qisi.utils.BeanToMap;
import com.qisi.utils.Config;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 告警输出数据内容控制
 */
@Data
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class KafkaAlertLog {
    String json;
    Rule rule;

    /**
     * @return 车机端埋点日志告警格式
     */
    public AlertManagerData genClientAlertData(){

        Map<String, Object> map = BeanToMap.jsonFlatten(json);

        SentryAlertData.SentryLabels labels = SentryAlertData
                .SentryLabels
                .builder()
                .alertName(map.getOrDefault(Config.KAFKA_LOG_BEHAVIOR_ID_KEY, "behaviorId").toString())
                .applicationId(map.getOrDefault(Config.KAFKA_LOG_VIN_KEY, "vin").toString())
                .applicationName(map.getOrDefault(Config.KAFKA_LOG_ALERT_NAME_KEY, "appPackage").toString())
                .link(Config.SENTRY_LINK)
                .eventId(map.getOrDefault(Config.KAFKA_LOG_BEHAVIOR_ID_KEY, "00000000").toString())
                .route(Route.WEBHOOK)
                .build();
        SentryAlertData.SentryAnnotations annotations = SentryAlertData.SentryAnnotations.builder().info(rule.getName()).summary("reason:" +rule.getExp()).build();

        return SentryAlertData.builder().labels(labels).annotations(annotations).build();
    }

    /**
     * @return 云端微服务日志告警格式
     */
    public AlertManagerData genCloudServiceLogAlertData(){

        Map<String, Object> map = BeanToMap.jsonFlatten(json);

        SentryAlertData.SentryLabels labels = SentryAlertData
                .SentryLabels
                .builder()
                .alertName(map.getOrDefault(Config.KAFKA_LOG_CLOUD_ALERTNAME_KEY, "cloud log alert").toString())
                .applicationId(map.getOrDefault(Config.KAFKA_LOG_APPLICATION_ID_KEY, "traceId").toString())
                .applicationName(map.getOrDefault(Config.KAFKA_LOG_BEHAVIOR_ID_KEY, "behaviorId").toString())
                .link(Config.SENTRY_LINK)
                .eventId(map.getOrDefault(Config.KAFKA_LOG_CLOUD_VIN_KEY, "vin").toString())
                .route(Route.WEBHOOK)
                .build();
        SentryAlertData.SentryAnnotations annotations = SentryAlertData.SentryAnnotations.builder().info(rule.getName()).summary("reason:" +rule.getExp()).build();

        return SentryAlertData.builder().labels(labels).annotations(annotations).build();
    }

    /**
     * @return 云端埋点日志告警
     *  {"vehicleModel": "CA6520H0EVXA_1", "oauthProviderId": "ximalaya_E115_HQ", "createDate": "1625044824376", "interfaceAccessEndTime": "2021-06-30 09:20:24.475", "vin": "TEST0000000115003", "interfaceAccessStartTime": "2021-06-30 09:20:24.575", "cp_provider_access_url": "http://a.cn/music/play", "supplier": "\u5176\u4ed6APP", "vehicleSeries": "H665", "version": "1.1", "statusComment": "\u5931\u8d25", "microservice": "OAuth", "statusCode": "1", "accounted": 43484}
     */
    public AlertManagerData genCloudEventAlertData(){

        Map<String, Object> map = BeanToMap.jsonFlatten(json);

        SentryAlertData.SentryLabels labels = SentryAlertData
                .SentryLabels
                .builder()
                .alertName(map.getOrDefault(Config.KAFKA_LOG_CLOUD_EVENT_ALERTNAME_KEY, "cloud event log alert").toString())
                .applicationId(map.getOrDefault(Config.KAFKA_LOG_CLOUD_EVENT_DEVICEID_KEY, "vehicleModel").toString())
                .applicationName(map.getOrDefault(Config.KAFKA_LOG_CLOUD_EVENT_OAUTHPROVIDER_ID_KEY, "oauthProviderId").toString())
                .link(Config.SENTRY_LINK)
                .eventId(map.getOrDefault(Config.KAFKA_LOG_CLOUD_EVENT_VIN_KEY, "vin").toString())
                .route(Route.WEBHOOK)
                .build();
        SentryAlertData.SentryAnnotations annotations = SentryAlertData.SentryAnnotations.builder().info(rule.getName()).summary("reason:" +rule.getExp()).build();

        return SentryAlertData.builder().labels(labels).annotations(annotations).build();
    }


    public AlertManagerData genSentryAlertData() {
        Map<String, Object> map = BeanToMap.jsonFlatten(json);

        SentryAlertData.SentryLabels labels = SentryAlertData
                .SentryLabels
                .builder()
                .alertName(map.getOrDefault(Config.SENTRY_LOG_APPLICATION_ID, "sentry exception log alert").toString())
                .applicationId(map.getOrDefault(Config.SENTRY_LOG_BEHAVIOR_ID, "behavior_id").toString())
                .applicationName(map.getOrDefault(Config.KAFKA_LOG_CLOUD_EVENT_OAUTHPROVIDER_ID_KEY, "oauthProviderId").toString())
                .link(Config.SENTRY_LINK)
                .eventId(map.getOrDefault(Config.SENTRY_LOG_VIN, "vin").toString())
                .route(Route.WEBHOOK)
                .build();
        SentryAlertData.SentryAnnotations annotations = SentryAlertData.SentryAnnotations.builder().info(rule.getName()).summary("reason:" +rule.getExp()).build();

        return SentryAlertData.builder().labels(labels).annotations(annotations).build();

    }

    /**
     * Sentry 日志返回到 Kafka 中
     */
    public boolean isSentryAlertData(){

        Map<String, Object> map = BeanToMap.jsonFlatten(json);

        String value = map.getOrDefault("common_data.behaviorId", "").toString();

        int len = value.length();

        if(len == 8 &&  ("001".equals(value.substring(5, 8)) ||  "0022".equals(value.substring(5, 8)))){
            log.debug("producer sentry alert data to kafka: vakue:{}", json);
            return true;
        }
        return true;

    }

}
